package edu.tyut.game.ui.learn

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import edu.tyut.game.R
import edu.tyut.game.utils.LogUtils

class DynastiesAdapter(private val context: Context, private val dynastiesList: List<String>, private val viewModel: LearnViewModel) : RecyclerView.Adapter<DynastiesAdapter.ViewHolder>(){

    private val tag = "DynastiesAdapter"

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val dynasty: TextView = view.findViewById(R.id.dynasty)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.dynasties_item, parent, false)
        val holder = ViewHolder(view)
        holder.dynasty.setOnClickListener {
            // 发请求完事
            val position = holder.absoluteAdapterPosition
            LogUtils.d(tag, "发送请求：$position")
            if (dynastiesList[position] == "推荐"){
                viewModel.getPoemOrderByClickCount()
            }else{
                viewModel.getPoemListByDynasty(dynastiesList[position])
            }
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val dynasties = dynastiesList[position]
        holder.dynasty.text = dynasties
    }

    override fun getItemCount() = dynastiesList.size
}